import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_penting.dart';
import 'package:flutter_learn_vi/src/net/dio_utils.dart';
import 'package:flutter_learn_vi/src/page/common/common_nodata_widget.dart';
import 'package:flutter_learn_vi/src/utils/navigator_utils.dart';
import 'package:flutter_learn_vi/src/utils/toast_utils.dart';

import 'PendingAddPage.dart';
import 'PendingListItemWidget.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2020/12/25.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///代码清单
class PendingItemPage extends StatefulWidget {
  @override
  _PendingItemPageState createState() => _PendingItemPageState();
}

class _PendingItemPageState extends State<PendingItemPage>
    with AutomaticKeepAliveClientMixin, SingleTickerProviderStateMixin {
  //页面保持状态
  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    super.initState();
    getNewsList();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("职言"),
        centerTitle: true,
        actions: [
          GestureDetector(
            onTap: () {
              NavigatorUtils.pushPage(
                context,
                PendingAddPage(),
              );
            },
            child: Container(
              padding: EdgeInsets.only(right: 10),
              child: Icon(Icons.add_box_rounded),
            ),
          ),
        ],
      ),
      backgroundColor: Colors.white,

      ///填充布局
      body: Container(
        width: double.infinity,
        height: double.infinity,
        child: RefreshIndicator(
          onRefresh: () {
            return refreshLoading();
          },
          child: buildListView(),
        ),
      ),
    );
  }

  Widget buildListView() {
    if (_list.length == 0) {
      return Center(
        child: NoDataWidget(
          clickCallBack: () {
            getNewsList(isRefresh: true);
          },
        ),
      );
    }
    return ListView.builder(
      itemCount: _list.length,
      itemBuilder: (BuildContext context, int index) {
        PentingBean bean = _list[index];
        return PendingListItemWidget(
          pentingBean: bean,
        );
      },
    );
  }

  List<PentingBean> _list = [];

  Future<bool> refreshLoading() async {
    await getNewsList(isRefresh: true);
    return true;
  }

  Future<bool> getNewsList({bool isRefresh = false}) async {
    ResponseInfo responseInfo = await DioUtils.instance.getRequest(
      url: HttpHelper.LISATPENDINALL,
    );
    if (responseInfo.success) {
      _list = [];
      Map map = responseInfo.data;
      List list = map["list"];
      list.forEach((element) {
        PentingBean newsBean = PentingBean.fromMap(element);
        _list.add(newsBean);
      });
    }
    if (mounted) {
      setState(() {});
      if (isRefresh) {
        ToastUtils.showToast("已刷新");
      }
    }

    return true;
  }
}
